package app.fugema.shop.main.goods.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyFlexboxLayoutManager;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.GoodsClassifyActivityBinding;
import app.fugema.shop.main.goods.adapter.GoodsClassifyAdapter;
import app.fugema.shop.main.goods.adapter.GoodsClassifyLeftAdapter;
import app.fugema.shop.main.goods.model.CategorieBean;
import app.fugema.shop.main.goods.model.CategoryLeftBean;
import app.fugema.shop.main.goods.model.CategoryPageBean;
import app.fugema.shop.main.goods.vm.GoodsVM;
import app.fugema.shop.main.setting.view.SearchActivity;

import java.util.ArrayList;
import java.util.List;


public class GoodsClassifyActivity extends BaseActivity<GoodsClassifyActivityBinding, GoodsVM> implements OnGetDataListener {

    public static void start(Context context) {
        context.startActivity(new Intent(context, GoodsClassifyActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_classify_activity);
    }

    private GoodsClassifyLeftAdapter leftAdapter;
    private GoodsClassifyAdapter adapter;

    private int categoryId;//当前的分类id

    private List<CategoryLeftBean> categoryLeftList;

    @Override
    protected void initView() {
        super.initView();
        leftAdapter = new GoodsClassifyLeftAdapter(){
            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, CategoryLeftBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);

                GoodsClassifyActivity.this.adapter.clear();
                GoodsClassifyActivity.this.adapter.setDataList( bean.getSubCategory() );
                //categoryId = bean.getId();
                //onGetData(1);
            }
        };
        leftAdapter.setRadio(true);
        binding.rvRecyclerViewLeft.setLayoutManager(new MyLinearLayoutManager(this));
        binding.rvRecyclerViewLeft.setAdapter(leftAdapter);

        adapter = new GoodsClassifyAdapter();
        binding.rvRecyclerViewRight.setLayoutManager(new MyFlexboxLayoutManager(this));
        binding.rvRecyclerViewRight.setAdapter(adapter);

        binding.etSelectText.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                SearchActivity.start(getActivity());
            }
        });
        binding.ivBack.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {

                finish();
            }
        });

        //viewUtils.setRefreshStateLayout(binding.rvRecyclerViewRight, this, this);
    }


    @Override
    protected void initData(Intent intent) {
        super.initData(intent);
        viewModel.categoryInfo().observe(this, new BaseViewObserver<LiveDataWrapper<CategoryPageBean>>(this) {
            @Override
            protected void onSuccess(LiveDataWrapper<CategoryPageBean> data)
            {
                categoryLeftList = getCategoryLeftList(data.data);
                leftAdapter.setDataList(categoryLeftList);

                adapter.setDataList( categoryLeftList.get(0).getSubCategory() );
                //categoryId = data.data.getCategory().get(0).getId();
                //onGetData(1);
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<CategoryPageBean> data)
            {
                return UIUtils.isEmpty(data.data.getCategory());
            }
        });
    }


    @Override
    public void onGetData(int pn)
    {
        /*
        viewModel.pageProd(categoryId, pn).observe(this, new BaseViewObserver<LiveDataWrapper<CouponTagBean>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<CouponTagBean> data) {
                viewUtils.setDataListRefreshLayout(adapter, data.data.getData().getCurrent(),data.data.getData().getSize(), data.data.getData().getRecords());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }
            @Override
            public boolean isEmptyData(LiveDataWrapper<CouponTagBean> data) {
                return UIUtils.isEmpty(data.data.getData()) || (data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords()));
            }
        });
        */
    }


    private List<CategoryLeftBean> getCategoryLeftList(CategoryPageBean categoryPageBean)
    {
        List<CategoryLeftBean> leftBeans = new ArrayList<>();

        List<CategorieBean> tempLeftList = categoryPageBean.getCategory();

        for (int i = 0; i < tempLeftList.size(); i++)
        {
            CategoryLeftBean  leftBean = new CategoryLeftBean();
            leftBean.setName( tempLeftList.get(i).getName() );
            List<CategorieBean> subLeftCates = getGroupContentCateBeans(tempLeftList.get(i).getSubCategories());
            leftBean.setSubCategory( subLeftCates );
            if(i == 0)
            {
                leftBean.setSelected(true);
            }
            leftBeans.add( leftBean );
        }
        return leftBeans;
    }


    private List<CategorieBean>  getGroupContentCateBeans(List<CategorieBean> subCategories)
    {
        List<CategorieBean> subCates = new ArrayList<>();

        for (CategorieBean subCategory : subCategories)
        {
            subCategory.setItemType(BaseItemTypeInterface.TYPE_GROUP);
            subCates.add( subCategory );

            if( subCategory.getSubCategories() != null && !subCategory.getSubCategories().isEmpty() )
            {
                for (CategorieBean tempCate : subCategory.getSubCategories())
                {
                    subCates.add( tempCate );
                }
            }
        }
        return subCates;
    }

}
